﻿using SuperX.Common.Helper;
using System;
using System.Collections.Generic;
using System.Text;

namespace SuperX.Common.AppConfiguration
{
    // 
    public class SystemCfgService
    {
        // 
        public SystemCfg SystemCfg { get; set; }

        // 
        private SystemCfgService()
        {
            this.Init();
        }

        // 
        private void Init()
        {
            try
            {
                SystemCfg = (JsonHelper.JsonDeserializeFromFile(this.FilePath, typeof(SystemCfg), Encoding.UTF8) as SystemCfg);
                if (SystemCfg == null)
                {
                    SystemCfg = new SystemCfg();
                }
            }
            catch
            {
                SystemCfg = new SystemCfg();
            }
        }

        // 
        public static SystemCfgService GetInstance()
        {
            if (_service == null)
            {
                object obj = SystemCfgService.obj;
                lock (obj)
                {
                    if (_service == null)
                    {
                        _service = new SystemCfgService();
                    }
                }
            }
            return _service;
        }

        // 
        public void Save(SystemCfg systemConfig)
        {
            JsonHelper.JsonSerializeToFile(systemConfig, this.FilePath, Encoding.UTF8);
        }

        // 
        private readonly string FilePath = "SystemConfig.appcfg";

        // 
        private static SystemCfgService _service;

        // 
        public static readonly object obj = new object();
    }
}
